package com.ocom.payconfig.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.card.EntityUpgradePlan;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payconfig.dao.IDaoUpgradePlan;
import com.ocom.payconfig.service.UpgradePlanService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class UpgradePlanServiceImpl extends ServiceImpl<IDaoUpgradePlan, EntityUpgradePlan> implements UpgradePlanService {

    @Autowired
    private IDaoUpgradePlan dao;

    @Override
    @DataSourceAspect
    public EntityUpgradePlan findByUpgradeCode(String upgradeCode) {
        QueryWrapper<EntityUpgradePlan> queryWrapper = Wrappers.query();
        queryWrapper.eq("upgrade_code", upgradeCode);
        queryWrapper.orderByDesc("plan_time");
        queryWrapper.last("limit 1");
        return this.getOne(queryWrapper);
    }

    @Override
    @DataSourceAspect
    public List<EntityUpgradePlan> findListByUpgradeCode(String upgradeCode) {
        QueryWrapper<EntityUpgradePlan> queryWrapper = Wrappers.query();
        if (StringUtils.isNotBlank(upgradeCode)) {
            queryWrapper.eq("upgrade_code", upgradeCode);
        }
        queryWrapper.orderByDesc("plan_time");

        return this.list(queryWrapper);
    }

    @Override
    @DataSourceAspect
    public boolean save(EntityUpgradePlan entity) {
        return super.save(entity);
    }
}
